package com.spring.sunflower.common

import android.content.pm.ActivityInfo
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.VideoPlayBean
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.main.adapter.TikTokAdapter
import com.spring.sunflower.util.Utils
import com.spring.sunflower.util.cache.PreloadManager
import com.spring.sunflower.video.OnViewPagerListener
import com.spring.sunflower.video.ViewPagerLayoutManager
import com.spring.sunflower.widget.EndlessRecyclerOnScrollListener
import com.spring.sunflower.widget.TikTokController
import xyz.doikki.videoplayer.exo.ExoMediaPlayer
import xyz.doikki.videoplayer.player.VideoView
import xyz.doikki.videoplayer.util.L


/**
 * Created by Kevin on 2021/8/22<br/>
 *
 * Describe:<br/>
 */
class VideoPlayActivity : BaseActivity<VideoPlayPresenter>(), CommonContract.VideoPlayView,
    OnViewPagerListener {
    private var rvRecyclerView: RecyclerView? = null
    private var mAdapter: TikTokAdapter? = null
    private var mData: MutableList<VideoPlayBean.DatasBean> = ArrayList()
    private var refreshLayout: SmartRefreshLayout? = null
    private var pageIndex = 1
    private var playIndex = 0
    private var mVideoView: VideoView<ExoMediaPlayer>? = null
    private var mController: TikTokController? = null
    private var mCurPos = 0
    override fun doSomethingBeforeOnCreate() {
        super.doSomethingBeforeOnCreate()
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

    override fun initView() {
        Constant.shouldShowCallDialog = false
        Constant.shouldShowLuckyMatchDialog = false
        playIndex = intent.getIntExtra("currentIndex", 0)
        mCurPos = playIndex
        val otherID = intent.getStringExtra("otherId")
        val userID = getField(Constant.KEY_USERID, "")
        rvRecyclerView = findViewById(R.id.rvRecyclerView)
        refreshLayout = findViewById(R.id.refreshLayout)
        mAdapter = TikTokAdapter(this, mData)
        val layoutManager = ViewPagerLayoutManager(this, OrientationHelper.VERTICAL)
        rvRecyclerView!!.layoutManager = layoutManager
        layoutManager.setOnViewPagerListener(this)
        rvRecyclerView!!.adapter = mAdapter
        val endlessScrollListener = object : EndlessRecyclerOnScrollListener(layoutManager) {
            override fun onLoadMore(current_page: Int) {
                mPresenter.getVideoList(
                    otherID!!,
                    (++pageIndex).toString()
                )
            }
        }

        rvRecyclerView!!.addOnScrollListener(endlessScrollListener)

        mVideoView = VideoView<ExoMediaPlayer>(this)
        mController = TikTokController(this)
        mVideoView?.let {
            it.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP)
            it.setLooping(true)
            it.setVideoController(mController!!)
        }
        refreshLayout!!.setEnableRefresh(false)
        refreshLayout!!.setEnableLoadMore(false)
//        refreshLayout!!.setOnLoadMoreListener {
//            mPresenter.getVideoList(
//                otherID!!,
//                (++pageIndex).toString()
//            )
//        }
        mPresenter.getVideoList(otherID!!, pageIndex.toString())

    }

    override fun getLayoutID(): Int = R.layout.activity_video_play

    override fun setPresenter(): VideoPlayPresenter = VideoPlayPresenter(this)
    override fun showVideoList(data: MutableList<VideoPlayBean.DatasBean>?) {
        data!!.reverse()
        if (pageIndex == 1) {
            mAdapter!!.refreshData(data)
            rvRecyclerView!!.scrollToPosition(playIndex)
        } else {
            mAdapter!!.addData(data!!)
        }
//        if (refreshLayout!!.isLoading) {
//            refreshLayout!!.finishLoadMore()
//        }
    }

    override fun onInitComplete() {
        printD("play=$playIndex")
        startPlay(playIndex)
    }

    override fun onPageRelease(isNext: Boolean, position: Int) {
        printD("onPageRelease position=$position")
        if (mCurPos == position) {
            mVideoView!!.release()
        }
    }

    override fun onPageSelected(position: Int, isBottom: Boolean) {
        printD("onPageSelected position=$position")
        if (mCurPos == position) return
        startPlay(position)
    }

    private fun startPlay(position: Int) {
        if (rvRecyclerView == null) return
        val itemView: View = rvRecyclerView!!.getChildAt(0)
        val viewHolder: TikTokAdapter.VideoHolder = itemView.tag as TikTokAdapter.VideoHolder
        mVideoView!!.release()
        Utils.removeViewFormParent(mVideoView)
        val item = mData.get(position)
        val playUrl: String =
            PreloadManager.getInstance(this).getPlayUrl(item.videoPath)
        L.i("startPlay: position: $position  url: $playUrl")
        mVideoView!!.setUrl(playUrl)
        //请点进去看isDissociate的解释
        mController!!.addControlComponent(viewHolder.mTikTokView, true)
        viewHolder.mPlayerContainer.addView(mVideoView, 0)
        viewHolder.loadingView.visibility = View.GONE
        mVideoView!!.start()
        mCurPos = position
    }

    override fun onPause() {
        super.onPause()
        mVideoView!!.pause()
    }

    override fun onResume() {
        super.onResume()
        mVideoView!!.resume()
    }

    override fun onDestroy() {
        super.onDestroy()
        mVideoView!!.release()
        Constant.shouldShowCallDialog = true
        Constant.shouldShowLuckyMatchDialog = true
    }

}